package com.caiyi.financial.nirvana.sms.service;

import com.caiyi.financial.nirvana.sms.base.service.BaseService;
import com.caiyi.financial.nirvana.sms.entity.SmsUserTemplate;
import com.caiyi.financial.nirvana.sms.enums.ReturnCodeEnum;
import com.caiyi.financial.nirvana.sms.enums.SmsUserTempleteStatusEnum;
import com.caiyi.financial.nirvana.sms.jedis.JedisKey;
import com.caiyi.financial.nirvana.sms.jedis.JedisService;
import com.caiyi.financial.nirvana.sms.mapper.SmsUserTemplateMapper;
import com.caiyi.financial.nirvana.sms.querybean.PageBean;
import com.caiyi.financial.nirvana.sms.querybean.UserTemplateQueryBean;
import com.youyu.common.api.Result;
import com.caiyi.financial.nirvana.sms.util.ValidateUtil;
import com.github.pagehelper.PageHelper;
import com.youyu.common.enums.BaseResultCode;
import com.youyu.common.exception.BizException;
import com.youyu.common.utils.YyAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SmsUserTemplateService extends BaseService<SmsUserTemplate> {

    @Autowired
    private SmsUserTemplateMapper smsUserTemplateMapper;

    public int addUserTemplate(SmsUserTemplate smsUserTemplate) {

        YyAssert.isFalse(!ValidateUtil.isNotNll(smsUserTemplate.getTemplate()), ReturnCodeEnum.PARAMS_ERROR.code, "模版不能为空");
        YyAssert.isFalse(smsUserTemplate.getTemplate().length() > 1024, ReturnCodeEnum.PARAMS_ERROR.code, "模版过长");
        YyAssert.isFalse(smsUserTemplate.getRemark() != null && smsUserTemplate.getRemark().length() > 255, ReturnCodeEnum.PARAMS_ERROR.code, "备注过长");
        int insertCount = 0;
        try {
            smsUserTemplate.setTemplate(smsUserTemplate.getTemplate().trim());
            smsUserTemplate.setStatus(SmsUserTempleteStatusEnum.AUDIT.index);
            List<String> templateList = smsUserTemplateMapper.selectTemplateListByAccount(smsUserTemplate.getAccount());
            YyAssert.isFalse(templateList.contains(smsUserTemplate.getTemplate()), ReturnCodeEnum.PARAMS_ERROR.code, "模板已存在");
            smsUserTemplate.setSize(smsUserTemplate.getTemplate().length());
            insertCount = this.insertByEntitySelective(smsUserTemplate);
            YyAssert.isFalse(insertCount == 0, ReturnCodeEnum.FAILED.code, "添加失败");
            logger.info("customer addUserTemplate success:... ");
        } catch (Exception e) {
            if (e instanceof BizException) {
                throw e;
            }
            logger.error("customer addUserTemplate error:...", e);
            YyAssert.isFalse(true, BaseResultCode.SYSTEM_ERROR);
        }
        return insertCount;
    }

    public int deleteUserTemplate(Integer id, String account) {
        int deleteCount = 0;
        YyAssert.isFalse(id == null, ReturnCodeEnum.PARAMS_ERROR.code, "ID不能为空");
        try {
            SmsUserTemplate smsUserTemplate = new SmsUserTemplate();
            smsUserTemplate.setId(id);
            smsUserTemplate = this.selectByPrimaryKey(smsUserTemplate);
            deleteCount = smsUserTemplateMapper.delByAccountAndId(account, id);
            YyAssert.isFalse(deleteCount == 0, ReturnCodeEnum.FAILED.code, "删除失败");
            //redis中删除用户模板
            if (deleteCount == 1 && smsUserTemplate.getStatus() == SmsUserTempleteStatusEnum.PASS.index) {
                String key = String.format(JedisKey.SMS_TEMPLATE, account);
                JedisService.srem(key, smsUserTemplate.getTemplate());
            }
            logger.info(" customer deleteByUserTemplateId account {}，id {},status {}", account, id, smsUserTemplate.getStatus());
        } catch (Exception e) {
            logger.error(" customer  deleteByUserTemplateId template  error.....", e);
            if (e instanceof BizException) {
                throw e;
            }
            YyAssert.isFalse(true, BaseResultCode.SYSTEM_ERROR);
        }
        return deleteCount;
    }

    public PageBean<SmsUserTemplate> getUserTemplateByParams(UserTemplateQueryBean bean) {
        PageBean<SmsUserTemplate> pageBean = null;
        PageHelper.startPage(bean.getPageNum(), bean.getPageSize());
        List<SmsUserTemplate> list = smsUserTemplateMapper.selectUserTemplateListByParams(bean);
        pageBean = new PageBean<>(list);
        return pageBean;
    }


}
